/*
 * Filename TestOrderMapping.java 2022年03月24日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.core.mapping;

import com.ehome.paymentcenter.api.pay.constant.PayStatus;
import com.ehome.paymentcenter.api.pay.domain.AmountDetail;
import com.ehome.paymentcenter.api.pay.domain.PayExtend;
import com.ehome.paymentcenter.api.pay.prepay.CashierPayQueryRspCO;
import com.ehome.paymentcenter.api.pay.qrcode.QrCodeReqCO;
import com.ehome.paymentcenter.api.pay.qrcode.QrCodeRspCO;
import com.ehome.paymentcenter.api.pay.scan.ScanPayReqCO;
import com.ehome.poct.core.api.constant.enums.OrderType;
import com.ehome.poct.core.api.dto.order.PrePlaceOrderDTO;
import com.ehome.poct.core.api.dto.order.TestOrderDTO;
import com.ehome.poct.core.api.dto.order.UnificationPayReqDTO;
import com.ehome.poct.core.api.vo.order.OrderItemPriceVO;
import com.ehome.poct.core.api.vo.order.OrderStatusVO;
import com.ehome.poct.core.api.vo.order.QrCodeVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @author wyc
 * @since 2022/03/24 17:21
 **/
@Mapper(componentModel = "spring", imports = {BigDecimal.class, PayStatus.class, OrderType.class})
public interface TestOrderMapping {
    @Mapping(target = "orderNo", source = "dto.testOrderNo")
    @Mapping(target = "title", source = "dto.note")
    @Mapping(target = "userId", source = "dto.clinicId")
    @Mapping(target = "payMoney", expression = "java(dto.getPayMoney().multiply(BigDecimal.valueOf(100)).longValue())")
    QrCodeReqCO to(TestOrderDTO dto, String timeoutExpress, String notifyUrl, PayExtend payExtend, String desc);

    List<AmountDetail> to(List<OrderItemPriceVO> items);

    @Mapping(target = "title", expression = "java(item.assemblyTitle())")
    @Mapping(target = "payAmount", expression = "java(item.getSalesPrice().multiply(BigDecimal.valueOf(100)).longValue())")
    @Mapping(target = "amount", expression = "java(item.getShowOfferMoney() ? item.getOriginPrice().multiply(BigDecimal.valueOf(100)).longValue() : item.getSalesPrice().multiply(BigDecimal.valueOf(100)).longValue())")
    @Mapping(target = "discountAmount", expression = "java(item.getShowOfferMoney() ? item.getOfferMoney().multiply(BigDecimal.valueOf(100)).longValue() : 0L)")
    AmountDetail to(OrderItemPriceVO item);

    @Mapping(target = "payUrl", source = "co.qrCode")
    @Mapping(target = "promptMsg", expression = "java(PayStatus.INIT.getDesc())")
    @Mapping(target = "orderStatus", expression = "java(PayStatus.INIT.getStatus())")
    QrCodeVO to(QrCodeRspCO co, BigDecimal money, Long expireTime);

    @Mapping(target = "status", expression = "java(isPay ? PayStatus.SUCCESS.getStatus() : PayStatus.INIT.getStatus())")
    @Mapping(target = "orderType", expression = "java(isPay ? OrderType.FREE.getType() : OrderType.NORMAL.getType())")
    @Mapping(target = "discountMoney", constant = "0")
    @Mapping(target = "couponMoney", constant = "0")
    @Mapping(target = "promotionMoney", constant = "0")
    @Mapping(target = "freightMoney", constant = "0")
    TestOrderDTO to(@MappingTarget TestOrderDTO dto, String outOrderNo, Boolean isPay);

    @Mapping(target = "isPay", expression = "java(entity.getStatus().equals(PayStatus.SUCCESS.getStatus()))")
    @Mapping(target = "promptMsg", expression = "java(payStatusToMsg(entity.getStatus()))")
    OrderStatusVO toStatus(TestOrderDTO entity);


    @Mapping(target = "orderNo", source = "dto.testOrderNo")
    @Mapping(target = "title", source = "dto.note")
    @Mapping(target = "userId", source = "dto.clinicId")
    @Mapping(target = "payMoney", expression = "java(dto.getPayMoney().multiply(BigDecimal.valueOf(100)).longValue())")
    ScanPayReqCO to(TestOrderDTO dto, String authCode, String timeoutExpress, String notifyUrl, PayExtend payExtend, String desc);

    @Mapping(target = "status", expression = "java(queryRspCO.getPayStatus().getStatus())")
    @Mapping(target = "isPay", expression = "java(queryRspCO.getPayStatus().equals(PayStatus.SUCCESS))")
    @Mapping(target = "promptMsg", expression = "java(payStatusToMsg(queryRspCO.getPayStatus().getStatus()))")
    @Mapping(target = "payType", expression = "java(queryRspCO.getPayType().getType())")
    OrderStatusVO toStatus(CashierPayQueryRspCO queryRspCO);

    default String payStatusToMsg(Integer status) {
        return Arrays.stream(PayStatus.values()).filter(i -> i.getStatus().equals(status)).map(PayStatus::getDesc).findFirst().orElse(null);
    }

    @Mapping(target = "orderNo", source = "orderDTO.testOrderNo")
    @Mapping(target = "showName", source = "orderDTO.note")
    @Mapping(target = "userId", source = "orderDTO.clinicId")
    @Mapping(target = "payMoney", expression = "java(orderDTO.getPayMoney().multiply(BigDecimal.valueOf(100)).longValue())")
    UnificationPayReqDTO toPayReqDTO(TestOrderDTO orderDTO, String timeoutExpress, String payNotifyUrl, String description);

    @Mapping(target = "orderNo", source = "testOrderNo")
    @Mapping(target = "clinicId", source = "clinicId")
    @Mapping(target = "agentId", source = "agentId")
    PrePlaceOrderDTO toPreOrder(TestOrderDTO orderDTO);
}